﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_Hotel_Management_System.Class;
using System.Data.SqlClient;
using System.Configuration;

namespace Delonix_Regia_Hotel_Management_System
{
    public partial class Update_And_Delete_of_Staff : Form
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand comm = new SqlCommand();
        List<Staff> staff = DBManager.getAllStaff();
        public Update_And_Delete_of_Staff()
        {
            InitializeComponent();
        }


        private void Update_and_Delete_of_Staff_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (LogInClass.position == "Administrator")
            {
                Main__Management_and_Administrator_ mainA = new Main__Management_and_Administrator_();
                this.Hide();
                mainA.Show();
            }
            else
            {
                Main__Receptionist_ mainR = new Main__Receptionist_();
                this.Hide();
                mainR.Show();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 F = new Form1();
            this.Hide();
            F.Show();
        }


        private void btnRetrieveAll_Click(object sender, EventArgs e)
        {
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.DataSource = staff;
            DataGridViewCheckBoxColumn checkBoxCol = new DataGridViewCheckBoxColumn();
            if (staff.Count > 0)
            {
                dataGridView1.Columns["staffId"].ReadOnly = true;
                dataGridView1.Columns["bankAccount"].Visible = false;
                dataGridView1.Columns["staffPassword"].Visible = false;
            }

            checkBoxCol.Name = "Check";
            checkBoxCol.HeaderText = " ";
            checkBoxCol.Width = 50;
            checkBoxCol.ThreeState = false;
            dataGridView1.Columns.Add(checkBoxCol);
            dataGridView1.Columns["Check"].DisplayIndex = 0;
            dataGridView1.Columns["Check"].Frozen = true;
            dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
            dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
        }

        private void Bind()
        {
            conn.ConnectionString = "Data Source = localhost; Initial Catalog= DelonixRegia; User ID = user; Password = Password_2013";
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * Staff", conn);
            //DataTable dt = new DataTable();
            //da.Fill(dt);
            conn.Close();
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string headerText = dataGridView1.Columns[e.ColumnIndex].HeaderText;
            string staffID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();


            if (headerText.Equals("phoneNum"))
            {
                int newInteger;
                if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    MessageBox.Show("Please fill in the " + headerText + " for StaffID: " + staffID,
                                        "Error",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation,
                                        MessageBoxDefaultButton.Button1);
                }

                else if (int.TryParse(e.FormattedValue.ToString(), out newInteger) == false)
                {
                    MessageBox.Show("Please enter only numbers for phone number.",
                                    "Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation,
                                    MessageBoxDefaultButton.Button1);
                }

            }

            else if (headerText.Equals("dob"))
            {
                DateTime newDateTime;
                if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    MessageBox.Show("Please fill in the " + headerText + " for StaffID: " + staffID,
                                        "Error",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation,
                                        MessageBoxDefaultButton.Button1);
                }
                else if (DateTime.TryParse(e.FormattedValue.ToString(), out newDateTime) == false)
                {
                    MessageBox.Show("Please enter the correct DOB format for " + staffID,
                                    "Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation,
                                    MessageBoxDefaultButton.Button1);
                }
            }
            else if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
            {
                MessageBox.Show("Please fill in the " + headerText + " for StaffID: " + staffID,
                                        "Error",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation,
                                        MessageBoxDefaultButton.Button1);
            }

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource == null)
            {
                MessageBox.Show("Please retrieve the staff informtion first!");
            }

            else
            {
                conn.ConnectionString = "Data Source = localhost; Initial Catalog= DelonixRegia; User ID = user; Password = Password_2013";
                conn.Open();

                for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
                {

                    SqlCommand cmd = new SqlCommand("Update Staff set staffId=@staffId,staffName=@staffName, homeAddress=@homeAddress, phoneNum=@phoneNum, position=@position Where(staffId=@staffId)", conn);
                    cmd.Parameters.AddWithValue("@staffId", dataGridView1.Rows[i].Cells[0].Value.ToString());
                    cmd.Parameters.AddWithValue("@staffName", dataGridView1.Rows[i].Cells[1].Value.ToString());
                    cmd.Parameters.AddWithValue("@homeAddress", dataGridView1.Rows[i].Cells[4].Value.ToString());
                    cmd.Parameters.AddWithValue("@phoneNum", dataGridView1.Rows[i].Cells[5].Value.ToString());
                    cmd.Parameters.AddWithValue("@position", dataGridView1.Rows[i].Cells[6].Value.ToString());
                    cmd.ExecuteNonQuery();

                }
                conn.Close();
                MessageBox.Show("Staff Information Successfully Updated!");
                Bind();
            }

        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
        {

        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToDeleteRows = true;
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                DataGridViewCell selected = r.Cells["Check"];
                if (selected.Value != null) //if checkbox is selected
                {
                    if (((bool)selected.Value) == true)
                    {
                        if (MessageBox.Show("Are you sure you want to delete the selected record(s)?",
                            "Delete warning", MessageBoxButtons.YesNo,
                            MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
                            == DialogResult.Yes)
                        {
                            conn.ConnectionString = "Data Source = localhost; Initial Catalog= DelonixRegia; User ID = user; Password = Password_2013";
                            SqlCommand delcmd = new SqlCommand();
                            string staffID = Convert.ToString(r.Cells["staffId"].Value);
                            string staffIdDELETE = Convert.ToString(r.Cells["staffId"].Value);
                            if (dataGridView1.Rows.Count > 1)
                            {
                                delcmd.CommandText = "DELETE [DelonixRegia].[dbo].[HouseKeeping] FROM [DelonixRegia].[dbo].[HouseKeeping] INNER JOIN [DelonixRegia].[dbo].[Staff] ON HouseKeeping.staffId = staff.staffId WHERE HouseKeeping.staffId=@staffId; DELETE FROM [DelonixRegia].[dbo].[Staff] WHERE StaffId=@staffId";
                                conn.Open();
                                delcmd.Connection = conn;
                                delcmd.Parameters.AddWithValue("@staffId", staffID);
                                delcmd.ExecuteNonQuery();
                                conn.Close();
                                MessageBox.Show("Staff Account Successfully Deleted");
                                staff.RemoveAt(dataGridView1.CurrentCell.RowIndex);
                            }
                            //Bind();
                        }

                        else if (((bool)selected.Value) == false)
                        {
                            MessageBox.Show("Please select the staff to be deleted!");
                            break;
                        }
                    }
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Staff staff = new Staff();
            string staffId = tbxStaffId.Text;
            if (DBManager.checkStaffId(staffId))
            {
                staff = DBManager.GetStaffID(staffId);
                BindingSource binding = new BindingSource();
                binding.DataSource = staff;
                dataGridView1.DataSource = binding;

                dataGridView1.Columns["staffId"].ReadOnly = true;
                dataGridView1.Columns["bankAccount"].Visible = false;
                dataGridView1.Columns["staffPassword"].Visible = false;
            }
            else if (string.IsNullOrEmpty(staffId))
            {
                MessageBox.Show("Please enter Staff ID!");
            }

            else
            {
                MessageBox.Show("Sorry, the staff ID not found.");
            }
        }






    }
}
